package com.cykj.finance.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.cykj.common.core.domain.R;
import com.cykj.common.core.utils.poi.ExcelUtil;
import com.cykj.common.core.web.controller.BaseController;
import com.cykj.common.core.web.domain.AjaxResult;
import com.cykj.common.core.web.page.TableDataInfo;
import com.cykj.common.log.annotation.Log;
import com.cykj.common.log.enums.BusinessType;
import com.cykj.common.security.annotation.RequiresPermissions;
import com.cykj.finance.domain.vo.FnCheckVO;
import com.cykj.finance.domain.vo.FnWithdrawVO;
import com.cykj.system.api.RemoteFileService;
import com.cykj.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cykj.finance.domain.FnWithdrawalRecords;
import com.cykj.finance.service.IFnWithdrawalRecordsService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 提现记录Controller
 *
 * @author 天才少年
 * @date 2025-08-15
 */
@RestController
@RequestMapping("/withdrawal")
public class FnWithdrawalRecordsController extends BaseController {
    @Autowired
    private IFnWithdrawalRecordsService fnWithdrawalRecordsService;


    @Resource
    private RemoteFileService remoteFileService;

    /**
     * 查询提现记录列表
     */
    @RequiresPermissions("finance:withdrawal:list")
    @GetMapping("/list")
    public TableDataInfo list(FnWithdrawalRecords bsWithdrawalRecords) {
        startPage();
        List<FnWithdrawalRecords> list = fnWithdrawalRecordsService.selectBsWithdrawalRecordsList(bsWithdrawalRecords);
        return getDataTable(list);
    }

    /**
     * 导出提现记录列表
     */
    @RequiresPermissions("finance:withdrawal:export")
    @Log(title = "提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FnWithdrawalRecords bsWithdrawalRecords) {
        List<FnWithdrawalRecords> list = fnWithdrawalRecordsService.selectBsWithdrawalRecordsList(bsWithdrawalRecords);
        ExcelUtil<FnWithdrawalRecords> util = new ExcelUtil<FnWithdrawalRecords>(FnWithdrawalRecords.class);
        util.exportExcel(response, list, "提现记录数据");
    }

    /**
     * 获取提现记录详细信息
     */
    @RequiresPermissions("finance:withdrawal:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fnWithdrawalRecordsService.selectBsWithdrawalRecordsById(id));
    }

    /**
     * 新增提现记录
     */
    @RequiresPermissions("finance:withdrawal:add")
    @Log(title = "提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(
            @RequestParam Long shopId,
            @RequestParam String alipayAccount,
            @RequestParam String accountHolder,
            @RequestParam String idCardNo,
            @RequestParam Double amount,
            @RequestParam String phone,
            @RequestParam MultipartFile idCardFront,
            @RequestParam MultipartFile idCardBack
    ) {

        R<SysFile> idCardFrontFile = remoteFileService.upload(idCardFront);
        R<SysFile> idCardBackFile = remoteFileService.upload(idCardBack);

        String idCardFrontPath = idCardFrontFile.getData().getPath();
        String idCardBackPath = idCardBackFile.getData().getPath();

        String idCardFrontUrl = idCardFrontFile.getData().getUrl();
        String idCardBackUrl = idCardBackFile.getData().getUrl();

        FnWithdrawVO fnWithdrawVO = new FnWithdrawVO();
        fnWithdrawVO.setShopId(shopId);
        fnWithdrawVO.setAlipayAccount(alipayAccount);
        fnWithdrawVO.setAccountHolder(accountHolder);
        fnWithdrawVO.setIdCardNo(idCardNo);
        fnWithdrawVO.setAmount(amount);
        fnWithdrawVO.setPhone(phone);
        fnWithdrawVO.setIdCardFront(idCardFrontPath);
        fnWithdrawVO.setIdCardBack(idCardBackPath);
        fnWithdrawVO.setIdCardFrontUrl(idCardFrontUrl);
        fnWithdrawVO.setIdCardBackUrl(idCardBackUrl);

        System.out.println(fnWithdrawVO);

        return toAjax(fnWithdrawalRecordsService.insertBsWithdrawalRecords(fnWithdrawVO));
    }

    /**
     * 修改提现记录
     */
    @RequiresPermissions("finance:withdrawal:edit")
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody FnWithdrawalRecords bsWithdrawalRecords) {

        return toAjax(fnWithdrawalRecordsService.updateBsWithdrawalRecords(bsWithdrawalRecords));
    }

    /**
     * 删除提现记录
     */
    @RequiresPermissions("finance:withdrawal:remove")
    @Log(title = "提现记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fnWithdrawalRecordsService.deleteBsWithdrawalRecordsByIds(ids));
    }

    @PutMapping("/check")
    public AjaxResult check(@RequestBody FnCheckVO fnCheckVO) {
        return success(fnWithdrawalRecordsService.checkIdentity(fnCheckVO));
    }
}
